<!-- This HTML file has been created by texi2html 1.29
     from ext2fs.texi on 3 August 1994 -->

<TITLE>Analysis of the Ext2fs structure - Blocks and Fragments</TITLE>
<P>Go to the <A HREF="ext2fs_1.html">previous</A>, <A HREF="ext2fs_3.html">next</A> section.<P>
<A NAME="IDX1"></A>
<A NAME="IDX2"></A>
<H1><A NAME="SEC2" HREF="ext2fs_toc.html#SEC2">Blocks and Fragments</A></H1>
<A NAME="IDX3"></A>
<P>
Blocks are the basic building blocks of a file system. The file system
manager requests to read or write from the disk are always translated to
a query to read or write an integral number of blocks from the disk.
<A NAME="IDX4"></A>
<P>
Some blocks on the file system are reserved for the exclusive use of the
superuser. This information is recorded in the <CODE>s_r_blocks_count</CODE>
member of the superblock structure. See section <A HREF="ext2fs_4.html#SEC4">Superblock</A> Whenever the total
number of free blocks becomes equal to the number of reserved blocks,
the normal users can no longer allocate blocks for their use. Only the
superuser may allocate new blocks. Without this provision for reserved
blocks, filling up the file system might make the computer unbootable.
Whenever the startup tasks would try to allocate a block, the computer
would crash. With reserved blocks, we ensure a minimum space for booting
and allowing the superuser to clean up the disk.
<A NAME="IDX5"></A>
<A NAME="IDX6"></A>
<A NAME="IDX7"></A>
<A NAME="IDX8"></A>
<A NAME="IDX9"></A>
<P>
This is all very simple. However, computer scientists like to
complicates things a bit. There are in fact two kinds of blocks, logical
blocks and physical blocks. The addressing scheme and size of these two
kind of blocks may vary. What happens is that when a request is made to
manipulate the range <SAMP>`[a,b]'</SAMP> of some file, this range is first
converted by the higher parts of the file system into a request to
manipulate an integral number of logical blocks: <SAMP>`a'</SAMP> is rounded
down to a logical block boundary and, <SAMP>`b'</SAMP> is rounded up to a
logical block boundary. Then, this range of logical blocks is converted
by lower parts of the file system into a request to manipulate an
integral number of physical blocks. The logical block size must be the
physical block size multiplied by a power of two <A NAME="FOOT2" HREF="ext2fs_foot.html#FOOT2">(2)</A>. So when going from logical to physical addressing
we just have to multiply the address by this power of two.
<A NAME="IDX10"></A>
<P>
The logical addresses of the file system goes from zero up to the total
number of blocks minus one. Block zero is the boot block and is usually
only accessed during special operations.
<P>
Now, the problem with blocks is that if we have a file that is not an
integral number of blocks, space at the end of the last block is wasted.
On average, one half block is wasted per file. On most file systems this
means a lot of wasted space. 
<A NAME="IDX11"></A>
<A NAME="IDX12"></A>
<A NAME="IDX13"></A>
<P>
To circumvent this inconvenience, the file system uses fragments. The
fragment size must be the physical block size multiplied by a power of
two <A NAME="FOOT3" HREF="ext2fs_foot.html#FOOT3">(3)</A>. A file is therefore a sequence of
blocks followed by a small sequence of consecutive fragments. When a file
has enough ending fragments to fill a block, those fragments are grouped
into a block. When a file is shortened, the last block may be broken into
many contiguous fragments.
<P>
The general relationship between sizes is:
<P>
<P>Go to the <A HREF="ext2fs_1.html">previous</A>, <A HREF="ext2fs_3.html">next</A> section.<P>
